package com.huiquan.vocab.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.huiquan.framework.base.ReturnData;
import com.huiquan.framework.utils.RequestParamUtil;
import com.huiquan.vocab.service.VocabElementOOaEqualsService;

@Controller
public class VocabElementOOaEqualsController {

	@Autowired
	private VocabElementOOaEqualsService vocabElementOOaEqualsService;

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/vocab/element_o_oa_equals_list.htm")
	public ModelAndView list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String tabid = req.getParameter("tabid");

		Map<String, Object> pageParam;
		if (tabid != null && !tabid.isEmpty()) {
			pageParam = new HashMap<>();
			pageParam.put("tabid", tabid);
			pageParam.put("exactSearchFlag", "0");
			pageParam.put("flag", "-1");

			req.getSession().setAttribute("pageParam", pageParam);
		} else {
			pageParam = (Map<String, Object>) req.getSession().getAttribute("pageParam");
		}

		// 获取页面参数，如果request中有值则修改session，否则从session中拿值
		String start = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "start");
		String word = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "word");
		String flag = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "flag");
		String exactSearchFlag = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "exactSearchFlag");

		return vocabElementOOaEqualsService.list(start, word, exactSearchFlag, flag);
	}

	/**
	 * 初始化o和oa一致表
	 * 
	 * @param req
	 * @param resp
	 * @return
	 * @throws IOException
	 */
	@ResponseBody
	@RequestMapping(value = "/vocab/element_o_oa_equals_init.do")
	public ReturnData init(HttpServletRequest req, HttpServletResponse resp) throws Exception {

		return vocabElementOOaEqualsService.init();
	}

	@ResponseBody
	@RequestMapping(value = "/vocab/element_o_oa_equals_check_page.do")
	public ReturnData checkPage(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String checkedIds = req.getParameter("checkedIds");
		String unCheckedIds = req.getParameter("unCheckedIds");
		
		return vocabElementOOaEqualsService.checkPage(checkedIds, unCheckedIds);
	}

}
